package com.pocketsweet.ui;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.pocketsweet.R;
import com.pocketsweet.chat.ui.activity.BaseActivity;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.fragments.LikePeopleFragment;
import com.pocketsweet.ui.fragments.LikeRecommendFragment;
import com.pocketsweet.ui.uilib.HeaderLayout;
import com.pocketsweet.ui.uilib.HeaderLayout.HeaderStyle;
import com.pocketsweet.ui.uilib.HeaderLayout.onLeftContainerListener;
import com.pocketsweet.ui.uilib.LoadingDailog;
import com.pocketsweet.ui.uilib.MyFragmentPagerAdapter;
import com.pocketsweet.ui.uilib.MyViewPager;
import com.pocketsweet.utils.ToolKits;

@ContentView(R.layout.activity_my_like)
public class MyLike extends BaseActivity implements View.OnClickListener {

	private HeaderLayout header;
	private MLUser mlUser = new MLUser();
	private String job;
	private static MyLike _instance;

	private LoadingDailog mLoadingDialog;
	private MyViewPager mPager;// 页卡内容
	private List<Fragment> listViews; // Tab页面列表
	private int currIndex = 0;// 当前页卡编号

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		_instance = this;
		ViewUtils.inject(this);
		mlUser = UserService.getCurrentUser();
		setHeaderFunction();
		setUpComponent();
		InitViewPager();
		mLoadingDialog = ToolKits.createLoadingDialog(MyLike.this, "登陆中...");
		mLoadingDialog.setText("加载中");
	}

	public void setHeaderFunction() {
		header = (HeaderLayout) findViewById(R.id.mainHeader);
		header.init(HeaderStyle.IMG_TITLE_TEXT);
		header.setRightText("");
		header.setLeftContainerClickListener(new onLeftContainerListener() {

			@Override
			public void onClick() {
				MyLike.this.finish();
			}
		});
		header.InitcursorView(false);
		header.setTvNewOnline("喜欢的人");
		header.setTvRecomend(" 喜欢的文章");

	}
	
	public static MyLike getInstance() {
		return _instance;
	}

	private void InitViewPager() {
		mPager = (MyViewPager) findViewById(R.id.vPager);
		listViews = new ArrayList<Fragment>();
		listViews.add(new LikePeopleFragment());
		listViews.add(new LikeRecommendFragment());
		mPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
				listViews));
		mPager.setCurrentItem(0);
		mPager.setOnPageChangeListener(new MyOnPageChangeListener1());

	}

	private void setUpComponent() {
		View linNewOnline = findViewById(R.id.linNewOnline);
		View linRecomend = findViewById(R.id.linRecomend);
		linNewOnline.setOnClickListener(this);
		linRecomend.setOnClickListener(this);
	}

	/**
	 * 页卡切换监听
	 */
	private class MyOnPageChangeListener1 implements OnPageChangeListener {
		public void onPageSelected(int arg0) {
			switch (arg0) {
			case 0:
				if (currIndex == 1) {
					header.slidleft();
				}
				break;
			case 1:
				if (currIndex == 0) {
					header.slidRight();
				}
				break;
			}
			currIndex = arg0;
		}

		public void onPageScrolled(int arg0, float arg1, int arg2) {
		}

		public void onPageScrollStateChanged(int arg0) {
		}
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	public void onClick(View arg0) {
		// TODO Auto-generated method stub
		switch (arg0.getId()) {
		case R.id.linNewOnline:
			mPager.setCurrentItem(0);
			break;
		case R.id.linRecomend:
			mPager.setCurrentItem(1);
			break;

		default:
			break;
		}
	}
}
